<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\user\controller;

use app\user\model\Users;
use mip\AdminApiBase;
use mip\Htmlp;
use think\Request;

class ApiAdminUser extends AdminApiBase
{
    protected $beforeActionList = ['start'];
    public function start() {
        $this->userModelNameSpace = 'app\user\model\Users';
    }
    
    public function userList(){
        $page = input('post.page');
        $limit = input('post.limit');
        $orderBy = input('post.orderBy');
        $order = input('post.order');
        $groupcode = input('post.groupcode');
        $keywords = input('post.keywords');
        
        if (!$page) {
            $page = 1;
        }
        if (!$limit) {
            $limit = 10;
        }
        if (!$orderBy) {
            $orderBy = 'reg_time';
        }
        if (!$order) {
            $order = 'desc';
        }

        $userList = model($this->userModelNameSpace)->getUserList($groupcode,$page,$limit,$orderBy,$order,null,$keywords);
        $userCount = model($this->userModelNameSpace)->getCount($groupcode,'', $keywords);

        return jsonSuccess('',['userList' => $userList,'total' => $userCount,'page' => $page]);
    }

    public function getUserInfo(){
        $uid = input('post.uid');
        if (!$uid) {
            return jsonSuccess('准备新增用户');
        }
        $userInfo = db('users')->where(array('uid'=>$uid))->find();
        if (!$userInfo) {
            return jsonError('不存在uid = '.$uid.' 的用户');
        }

        $userGroup = db('users_group')->where('group_code',$userInfo['group_code'])->find();
        $userInfo['groupcodeStr']=$userGroup['name'];

        return jsonSuccess('',$userInfo);

    }

    public function userAddOrEdit(Request $request)
    {
        if (Request::instance()->isPost()) {
            $username = htmlp(input('post.username'));
            $nickname = htmlp(input('post.nickname'));
            $email = htmlp(input('post.email'));
            $mobile = htmlp(input('post.mobile'));
            $qq = htmlp(input('post.qq'));
            $sex = htmlp(input('post.sex'));
            $uid = input('post.uid');
            $groupcode = input('post.groupcode');
            $head_pic = input('post.head_pic');
            $password = input('post.password');

            $publishStatus = input('post.publishStatus');

            if ($publishStatus=='add') {
                // 新增
                if($groupcode==null||$groupcode==''){
                    return jsonError('创建用户必须必须确定用户组');
                }

                $data['uid'] = uuid();
                $data['group_code'] = $groupcode;
                $data['username'] = $username;
            }else{
                // 编辑
                if(!$userInfo = Users::where('uid',$uid)->find()){
                    return jsonError('在编辑用户的时候，用户uid='.$uid.'不存在');
                }
            }

            $dbgroupId = db('usersGroup')->where('group_code',$groupcode)->field('group_id')->find();
            if($dbgroupId!=null){
                $data['group_id'] = $dbgroupId['group_id'];
            }

            if (!$sex) {
                $sex = 1;
            }
            $data['qq'] = $qq;
            $data['sex'] = $sex;
            $data['nickname'] = $nickname;
            $data['head_pic'] = $head_pic;

            if($username=='' && $mobile==''){
                return jsonError('用户名或者手机号码不能全部为空');
            }

            if($uid==null||$uid==''){
                if($password==null||$password==''){
                    return jsonError('新增用户的时候，必须填写密码');
                }
            }

            if($password!=null&& $password!=''){
                $data['salt'] = create_salt(8);
                $npassword1 = create_md5($password,$data['salt']);
                $data['password'] = $npassword1;
            }

            if ($mobile) {
                if (!preg_match("/^1[34578]\d{9}$/", $mobile)) {
                    return jsonError('手机号码输入有误');
                }
                if ($mobile!=$userInfo['mobile'] &&Users::getByMobile($mobile)) {
                    return jsonError('手机已存在');
                }
            }
            $data['mobile'] = $mobile;

            $rules = [
                'qq' => 'number',
                'email' => 'email',
            ];
            $msg = [
                'qq' => 'QQ号格式错误',
                'email' => '邮箱格式错误',
            ];
            $result = $this->validate($request->param(), $rules, $msg);
            if (!$result) {
                return $result;
            }
            if($email){
                if ($email != $userInfo['email'] AND Users::getByEmail($email)) {
                    return jsonError('邮箱已存在');
                }
            }
            $data['email'] = $email;

            if ($data) {
                if($publishStatus=='add'){
                    $usersInfo = Users::insert($data);
                    return jsonSuccess('用户保存成功');
                }else{
                    $usersInfo = Users::update($data,['uid'=>$uid]);
                    return jsonSuccess('用户修改成功');
                }

            }


        }
    }

    public function userDel(){
        $uid=input('post.uid');

        db('users')->where('uid',$uid)->delete();
        return jsonSuccess('删除成功');

    }




}